//$URL: http://dasding.googlecode.com/svn/branches/testzeugs/data/dao/couchDB/src/test/java/de/piratech/dasding/data/couchimpl/generator/CreateTestData.java $
//$Id: CreateTestData.java 88 2012-11-05 22:54:19Z amuthmann@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data.couchimpl.generator;

import de.piratech.dasding.data.Action;
import de.piratech.dasding.data.ActionCategory;
import de.piratech.dasding.data.Event;
import de.piratech.dasding.data.EventCategory;
import de.piratech.dasding.data.User;
import de.piratech.dasding.data.database.ActionCategoryData;
import de.piratech.dasding.data.database.ActionData;
import de.piratech.dasding.data.database.DataType;
import de.piratech.dasding.data.database.DatabaseException;
import de.piratech.dasding.data.database.EventCategoryData;
import de.piratech.dasding.data.database.EventData;
import de.piratech.dasding.data.database.UserData;
import de.piratech.dasding.data.database.couch.CouchImpl;
import java.util.ArrayList;
import java.util.List;
import org.dummycreator.ClassBindings;
import org.dummycreator.DummyCreator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class CreateTestData {

  @Autowired
  CouchImpl couch;
  List<EventCategory> eventCategories = new ArrayList<EventCategory>();
  List<ActionCategory> actionCategories = new ArrayList<ActionCategory>();

  @Test
  public void createTestData() throws DatabaseException {
    createActionCategories(couch);
    createEventCategories(couch);
    //createUsers(couch);
    createEvents(couch);
    createActions(couch);
  }

  private void createEventCategories(CouchImpl couch) throws DatabaseException {
    EventCategoryData eventCategoryData = couch.getEventCategoryData();
    List<EventCategory> categories = eventCategoryData.getCategories();
    for (EventCategory eventCategory : categories) {
      eventCategoryData.deleteEventCategory(eventCategory);
    }

    EventCategory event1 = new EventCategory();
    event1.setName("Demo");
    event1.setDescription("Etwas ist scheiße und diese Veranstaltung soll darauf aufmerksam machen.");
    event1.setVerified(true);

    EventCategory event2 = new EventCategory();
    event2.setName("Socializing");
    event2.setDescription("Ein Treffen bei dem man einfach nur eine schöne Zeit zusammen verbringt.");
    event2.setVerified(true);

    EventCategory event3 = new EventCategory();
    event3.setName("Barcamp");
    event3.setDescription("Treffen zu einem bestimmten Thema um darüber zu diskutieren und sich auszutauschen.");
    event3.setVerified(true);

    EventCategory event4 = new EventCategory();
    event4.setName("Was illegales");
    event4.setDescription("Das ist eine Eventkategory, die garantiert niemals freigeschalten und für alle zugänglich gemacht wird.");
    event4.setVerified(false);

    eventCategoryData.createEventCategory(event1);
    eventCategories.add(event1);
    eventCategoryData.createEventCategory(event2);
    eventCategories.add(event2);
    eventCategoryData.createEventCategory(event3);
    eventCategories.add(event3);
    eventCategoryData.createEventCategory(event4);
    eventCategories.add(event4);
  }

  private void createActionCategories(CouchImpl couch) throws DatabaseException {
    ActionCategoryData actionCategoryData = couch.getActionCategoryData();
    List<ActionCategory> categories = actionCategoryData.getCategories();
    for (ActionCategory actionCategory : categories) {
      actionCategoryData.deleteActionCategory(actionCategory);
    }

    ActionCategory cat1 = new ActionCategory();
    cat1.setName("Kochen");
    cat1.setDescription("Lebenmittel klein schnibbeln und in einen Topf tun. Den Topf auf eine heiße Herdplatte stellen und so lange umrühren bis alles weich ist");
    cat1.setVerified(true);

    ActionCategory cat2 = new ActionCategory();
    cat2.setName("Transportieren");
    cat2.setDescription("Etwas wahrscheinlich großes oder schweres von A nach B fahren.");
    cat2.setVerified(true);

    ActionCategory cat3 = new ActionCategory();
    cat3.setName("Flyer verteilen");
    cat3.setDescription("Alle Flyer verteilen. Egal an wen.");
    cat3.setVerified(true);

    ActionCategory cat4 = new ActionCategory();
    cat4.setName("Was illegales");
    cat4.setDescription("Das ist eine Actionkategory, die garantiert niemals freigeschalten und für alle zugänglich gemacht wird.");
    cat4.setVerified(false);

    actionCategoryData.createActionCategory(cat1);
    actionCategories.add(cat1);
    actionCategoryData.createActionCategory(cat2);
    actionCategories.add(cat2);
    actionCategoryData.createActionCategory(cat3);
    actionCategories.add(cat3);
    actionCategoryData.createActionCategory(cat4);
    actionCategories.add(cat4);

  }

  private void createActions(CouchImpl couch) throws DatabaseException {
    ActionData actionData = couch.getActionData();
    List<Action> actions = actionData.getActions();
    for (Action action : actions) {
      actionData.deleteAction(action);
    }

    createEvents(couch, actionCategories.get(0), 3);
    createEvents(couch, actionCategories.get(1), 3);
    createEvents(couch, actionCategories.get(2), 3);
    createEvents(couch, actionCategories.get(3), 3);
  }

  private void createEvents(CouchImpl couch, ActionCategory category, int count) throws DatabaseException {
    ActionData actionData = couch.getActionData();
    for (int i = 0; i < count; i++) {
      Action action = new DummyCreator(
              ClassBindings.defaultBindings()).create(Action.class);
      action.setId(null);
      action.setRevision(null);
      action.setParticipants(null);
      action.setContact((User) null);
      action.setResponsible((User) null);
      action.setCategory(category.getId());
      actionData.createAction(action);
    }
  }

  private void createEvents(CouchImpl couch) throws DatabaseException {
    EventData eventData = couch.getEventData();
    List<Event> events = eventData.getEvents();
    for (Event event : events) {
      eventData.deleteEvent(event);
    }
    createEvents(couch, eventCategories.get(0), 3);
    createEvents(couch, eventCategories.get(1), 3);
    createEvents(couch, eventCategories.get(2), 3);
    createEvents(couch, eventCategories.get(3), 3);

  }

  private void createEvents(CouchImpl couch, EventCategory category, int count) throws DatabaseException {
    EventData eventData = couch.getEventData();
    for (int i = 0; i < count; i++) {
      Event event = new DummyCreator(
              ClassBindings.defaultBindings()).create(Event.class);
      event.setId(null);
      event.setRevision(null);
      event.setActions(null);
      event.setContact((User) null);
      event.setResponsible((User) null);
      event.setPredecessor(null);
      event.setProspects(null);
      event.setDataType(DataType.EVENT);
      event.setCategory(category.getId());
      eventData.createEvent(event);
    }
  }

  private void createUsers(CouchImpl couch) throws DatabaseException {
    UserData userData = couch.getUserData();
    for (int i = 0; i < 10; i++) {
      User user = new DummyCreator(ClassBindings.defaultBindings())
              .create(User.class);
      user.setId(null);
      user.setRevision(null);
      user.setActionApplications(null);
      user.setInterestedIn(null);
      user.setDataType(DataType.USER);
      userData.createUser(user);
    }
  }
}
